package com.atguigu.gulimall.search.thread;

import lombok.SneakyThrows;

import java.util.concurrent.TimeUnit;

/**
 * @ ClassName SpinkLockTest
 * @ Description TODO
 * @ Author admin
 * @ Date 2021/3/17 10:33
 * @ Version 1.0
 */
public class SpinkLockTest implements Runnable{

    SpinLock locl = new SpinLock();

    public void get() throws InterruptedException {
        locl.lock();
        System.out.println("当前线程"+Thread.currentThread().getId()+" 在get方法中睡两秒");
//        Thread.sleep(1000);
        set();
        locl.unlock();
    }
    public  void set() throws InterruptedException {
//        locl.lock();
        System.out.println("当前线程"+Thread.currentThread().getId()+" 在set方法中睡一秒");
//        locl.unlock();
    }
    private int m = 0;

    private void increment() throws InterruptedException {
        locl.lock();
        m++;
        locl.unlock();
    }

    @SneakyThrows
    @Override
    public void run() {
        get();
//        increment();
    }

    public static void main(String[] args) throws InterruptedException {
        SpinkLockTest spinkLockTest = new SpinkLockTest();
        new Thread(spinkLockTest).start();
        new Thread(spinkLockTest).start();
//        new Thread(new SpinkLockTest()).start();
//        new Thread(new SpinkLockTest()).start();
//        new Thread(new SpinkLockTest()).start();
//        for(int i = 0;i<200000;i++){
//            new Thread(spinkLockTest).start();
//        }
//        TimeUnit.SECONDS.sleep(2);
//        System.out.println("m="+spinkLockTest.m);
    }
}
